#include "mysql.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
#include <QDateTime>

MySql *MySql::obj = nullptr;
MySql::MySql(QObject *parent) : QObject(parent)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("smarthome.db");
    db.open();
}

MySql *MySql::getObject()
{
    if(obj == nullptr)
        obj = new MySql;
    return obj;
}

bool MySql::userLogin(QString username, QString password)
{
    if(username.isEmpty())
        return false;
    QSqlQuery query;
    bool ok = query.exec(
        QString("SELECT password FROM t_user_info WHERE username='%1';")
                .arg(username)
                );
    if(!ok)
        return false;
    while(query.next())
    {
        if(query.value(0) == password)
            return true;
    }
    return false;
}

int MySql::userRegister(QString username, QString password, QString phone, QString email)
{
    if(username.isEmpty())
        return 1;
    //密码长度
    if(password.length() < 8)
        return 2;
    //手机号格式
    if(phone.length() != 11)
        return 3;
    if(phone.at(0) != '1')
        return 3;
    //用户名存在
    bool ok;
    QSqlQuery query;
    ok = query.exec(
            QString("SELECT COUNT(*) FROM t_user_info WHERE username='%1';")
                .arg(username)
                );
    if(!ok)
    {
        return 1;
    }
    if(query.next())
    {
        if(query.value(0).toInt() > 0)
            return 1;
    }
    //判断手机号是否存在
    ok = query.exec(
            QString("SELECT COUNT(*) FROM t_user_info WHERE phone='%1';")
                .arg(phone)
                );
    if(!ok)
    {
        return 4;
    }
    if(query.next())
    {
        if(query.value(0) > 0)
            return 4;
    }
    //判断邮箱是否存在
    ok = query.exec(
            QString("SELECT COUNT(*) FROM t_user_info WHERE email='%1';")
                .arg(email)
                );
    if(!ok)
    {
        return 5;
    }
    if(query.next())
    {
        if(query.value(0) > 0)
            return 5;
    }
    //插入用户数据
    ok = query.exec(
            QString("INSERT INTO t_user_info "
                    "(username, password, phone, email) "
                    "VALUES('%1', '%2', '%3', '%4');")
                .arg(username)
                .arg(password)
                .arg(phone)
                .arg(email)
                );
    if(!ok)
    {
        qDebug() << query.lastError().text();
        return 6;
    }
    return 0;
}

bool MySql::saveData(int co2, bool smoke, int pm2_5, int illumination, bool inf1, bool inf2, bool methane, int ultraviolet_rays, bool fire, bool antiphoto, bool reflection, double temperature, double humidity)
{
    //1.获取最大的id
    bool ok;
    QSqlQuery query;
    ok = query.exec(
            "SELECT MAX(id) FROM t_device_data;"
                );
    if(!ok)
        return false;
    if(!query.next())
        return false;
    int id = query.value(0).toInt() + 1;
    //2.插入数据
    ok = query.exec(
            QString("INSERT INTO t_device_data "
            "(id, date_time, co2, smoke, pm2_5, "
            "illumination, inf1, inf2, methane, "
            "ultraviolet_rays, fire, antiphoto, "
            "reflection, temperature, humidity) "
            "VALUES(%1, '%2', %3, %4, %5, %6, %7, "
            "%8, %9, %10, %11, %12, %13, %14, %15);")
            .arg(id)
            .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
            .arg(co2).arg(smoke ? 1 : 0).arg(pm2_5).arg(illumination)
            .arg(inf1 ? 1 : 0).arg(inf2 ? 1 : 0)
            .arg(methane ? 1 : 0).arg(ultraviolet_rays)
            .arg(fire ? 1 : 0).arg(antiphoto ? 1 : 0)
            .arg(reflection ? 1 : 0).arg(temperature)
            .arg(humidity)
                );
    if(!ok)
    {
        qDebug() << query.lastError().text();
        return false;
    }
    return true;
}






